package com.example.enterprisemanager.mvvm.fragment.account.login;

import com.example.enterprisemanager.base.BaseModel;
import com.example.enterprisemanager.mvvm.ben.User;
import com.orhanobut.logger.Logger;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class LoginModel extends BaseModel {
    private Disposable mLoginDisposable;
    /**
     * 账号密码登录
     */
    public void login(User user, Consumer<User> success, Consumer<Exception> fail) {
        mLoginDisposable = user.login(new SaveListener<User>() {
            @Override
            public void done(User bmobUser, BmobException e) {
                try {
                    if (e == null) {
                        User user = BmobUser.getCurrentUser(User.class);
                        success.accept(user);
                    } else {
                        fail.accept(e);
                    }
                } catch (Exception exception) {
                    Logger.d(exception.getMessage());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mLoginDisposable != null) {
            mLoginDisposable.dispose();
        }
    }
}
